
var cloudwalkobj;
var width=1;
var height=1;
var CW_StrSeverIp = "120.25.161.56:7000";
var CW_appID = "user";
var CW_appSecret = "12345";
var CW_nfaceValue = 0.7;
var imgAData = "";
var imgBData = "";

function createPlugin(targetE) {
	cloudwalkobj = document.createElement("object");
	cloudwalkobj.width = width;
	cloudwalkobj.height = height;
	cloudwalkobj.id = "CloudWalkSDKPlugin";
	
	var bDet=BrowserDetect.browser;
	if("Explorer" == bDet)
	{
		cloudwalkobj.classid = "CLSID:B1597418-A51E-4140-8698-EE865439755C";	//IE
	}
	else if("Firefox" == bDet)
	{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";		//Firefox
	}
	else if("Chrome" == bDet)
	{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";		//Chrome
	}
	else
	{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";
	}

	targetE.appendChild(cloudwalkobj);
	
	if(!cloudwalkobj.valid)
	{
		alert("插件未安装");
		return false;
	}
	
	registerCallBack(cloudwalkobj, "cwFaceCompareCallback", compareByBased64Event);
	var config = cloudwalkobj.cwGetConfig();
	PraseConfig(config);
	return true;
}


function compareByBased64Event(jsonStr) {
	
	//EnableButton();
	hideLoading();
	var jsonObj = $.parseJSON(jsonStr);
	var str = "";
	var source = "";
	if(jsonObj.result == 0)
	{
		source += "人脸相似度：";
		var TempSource = num(jsonObj.score);
		source += TempSource;
		if(TempSource >= CW_nfaceValue)
		{
			str = "是同一个人";
		}
		else
		{
			str = "不是同一个人";
		}	
	}
	else
	{	
		if(jsonObj.result == CW_couldntConnect)
		{
			str += "网络异常！";
		}
		else if(jsonObj.result == EnumLocalNetErrorSerAddr)
		{
			str += "无法解析人脸识别服务主机地址！";
		}
		else if(jsonObj.result == EnumLocalNetErrorConSer)
		{
			str += "无法连接到人脸识别服务主机！";
		}
		else if(jsonObj.result == CW_TimeOut)
		{
			str += "访问人脸识别服务主机超时！";
		}
		else if(jsonObj.result == CW_IMGQUALITY_NOT_GOOD)
		{
			str += "识别失败照片质量太差";
		}
		else if(jsonObj.result == CW_NORIGHT)
		{
			str += "没有权限";
		}
		else
		{
			str += "人脸比对失败！失败信息：";
			str += jsonObj.resultInfo;
		}
	}
	showMessageInfo(source, str);
}

//异步方法（不会阻塞浏览器）
function click_CompareByBase64()
{	
	if(imgAData.length ==0)
	{
		alert("请加载第一张图片");
		return;
	}
	if(imgBData.length ==0)
	{
		alert("请加载第二张图片图片");
		return;
	}
	
	showMessageInfo("","人脸比对中...");
	showLoading();
	var nret = cloudwalkobj.cwFaceCompare(CW_StrSeverIp, CW_appID, CW_appSecret,imgAData, imgBData);
	if(nret != 0)
	{
		hideLoading();
		document.getElementById("score").innerHTML = "";
		document.getElementById("info").innerHTML = "";
		$("#question").css("display","block");
	}
}	
	

function click_GetBase64ByLocalFileA()
{
	var imgAPath=cloudwalkobj.cwSelectFile();
	if(imgAPath.length == 0)
	{
		return;
	}
	
	document.getElementById("fileText").value="";
	document.getElementById("imgA").src = '../images/tianjiazhaopian.png';
	
	imgAData=cloudwalkobj.cwGetBase64ByLocalFile(imgAPath);
	if(imgAData == "" || imgAData == null)
	{
		alert("加载图片失败");
		return;
	}
	
	document.getElementById("fileText").value=imgAPath;
	var imgObj = document.getElementById("imgA");
	showImg(imgObj, imgAPath, imgAData);
	
	document.getElementById("score").innerHTML = "";
	document.getElementById("info").innerHTML = "";
	$("#question").css("display","block");
}

function click_GetBase64ByLocalFileB()
{
	var imgBPath=cloudwalkobj.cwSelectFile();
	if(imgBPath.length == 0)
	{
		return ;
	}
	
	document.getElementById("fileText2").value="";
	document.getElementById("imgB").src = '../images/tianjiazhaopian.png';
	
	imgBData=cloudwalkobj.cwGetBase64ByLocalFile(imgBPath);
	if(imgBData == "" || imgBData == null)
	{
		alert("加载图片失败");
		return;
	}
	
	document.getElementById("fileText2").value=imgBPath;
	var imgObj = document.getElementById("imgB");
	showImg(imgObj, imgBPath, imgBData);
	
	document.getElementById("score").innerHTML = "";
	document.getElementById("info").innerHTML = "";
	$("#question").css("display","block");
}

function openCramera()
{
	var nRet = cloudwalkobj.cwStartCamera(0,0);
	
	if(nRet != 0)
	{
		var msg = "打开摄像头失败错误码:"  + nRet;
		alert(msg);
	}
	
	return nRet
}

function closeCramera()
{
	cloudwalkobj.cwStopCamera();
}

function showMessageInfo(source,info)
{
	$("#question").css("display","none");
	document.getElementById("score").innerHTML = source;
	document.getElementById("info").innerHTML = info;
}

function takePicture()
{
	var Tempimg = cloudwalkobj.cwTakePictures();
	if(Tempimg == null || Tempimg.length == 0)
	{
		alert("拍照失败！");
		return;
	}
	if(ImgObjName == "imgA")
	{
		imgAData = Tempimg;
		document.getElementById("fileText").value = "";
	}
	else if(ImgObjName == "imgB")
	{
		imgBData = Tempimg;
		document.getElementById("fileText2").value= "";
	}
	
	var faceimg = document.getElementById(ImgObjName);
	if(isIE6_8())
	{
		var saveDir = "C:/cloudwalk/"; 					//图片保存的所在目录
		var filename = new Date().getTime()+".jpg";		//图片名称，如果文件名已存在则直接覆盖
		//var filename = "bestFace.jpg";				//文件名固定时，使用img.src = path;不能及时更新，还是显示上一张
		var filepath = saveDir + filename;
		var nret = cloudwalkobj.cwBase64ToFile(Tempimg, saveDir, filename);		//Base64保存为本地图片
		faceimg.src = 'file:///'+filepath;			//在Firefox下面必须添加file:///前缀才可以正确显示图片，IE有无皆可
	}
	else
	{
		faceimg.src = "data:image/jpeg;base64,"+ Tempimg;
	}
	
	document.getElementById("score").innerHTML = "";
	document.getElementById("info").innerHTML = "";
	$("#question").css("display","block");
}

function PraseConfig(strJson)
{
	var jsonObj = $.parseJSON(strJson);
	if(jsonObj != "null")
	{
	 CW_StrSeverIp = jsonObj["ip"];
	 CW_appID =  jsonObj["appID"];
	 CW_appSecret =  jsonObj["appSecret"];
	 CW_nfaceValue = parseFloat(jsonObj["faceValue"]);
	 
	 var nret = cloudwalkobj.cwInit(jsonObj["License"]);
	 if(nret != 0)
	 {
		var msg = "初始化SDK失败错误码：" + nret;
		alert(msg);
		return;
	 }
	}
	
}

function registerCallBackEach()
{
	//兼容谷歌
	//谷歌浏览器移除插件会析构，添加会创建
	var bDet=BrowserDetect.browser;
	if("Chrome"==bDet)
	{
		registerCallBack(cloudwalkobj, "cwFaceCompareCallback", compareByBased64Event);
	}
}



